﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Coffee_Restaurant.DAL;
using Coffee_Restaurant.Database;
using Coffee_Restaurant.Models;
using MetroFramework;
using MetroFramework.Forms;

namespace Coffee_Restaurant.Forms.Items
{
    public partial class AddCategory : MetroForm
    {
        //event edit
        public delegate void ButtonSaveEventHandler(string id, string name);
        public event ButtonSaveEventHandler BtnSave;

        public AddCategory()
        {
            InitializeComponent();
            //load style default
            var style = AppSettingDa.GetValue("Style");
            if (style != null)
            {
                this.Style = (MetroColorStyle)(int.Parse(style));
            }
        }

        private void AddItem_Load(object sender, EventArgs e)
        {
        }

        private void metroButton1_Click(object sender, EventArgs e)
        {
            if (ValidateForm())
            {
                try
                {
                    using (var db = new DatabaseEntities())
                    {
                        //add to database
                        var guid = Guid.NewGuid().ToString();
                        db.ItemCategories.Add(new ItemCategory() { Name = txtName.Text, Description = txtDescription.Text, Date = DateTime.Now, GuiId = guid });
                        if (db.SaveChanges() > 0)
                        {
                            var model = db.ItemCategories.FirstOrDefault(a => a.GuiId == guid);
                            if (model != null)
                            {
                                BtnSave(model.ItemCategoriesId.ToString(), model.Name);
                            }
                            this.Hide();
                        }
                    }
                }
                catch (Exception)
                {
                    BtnSave(null, null);
                }
            }

        }

        bool ValidateForm()
        {
            if (!ValidateHelper.IsString(txtName.Text, 2))
            {
                txtName.Focus();
                return false;
            }
            return true;
        }

        private void metroButton2_Click(object sender, EventArgs e)
        {
            this.Hide();
        }
    }
}
